package com.ruoyi.ihome.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ihome.domain.Apartment;
import com.ruoyi.ihome.service.IApartmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公寓管理Controller
 * 
 * @author zy
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/ihome/apartment")
public class ApartmentController extends BaseController
{
    @Autowired
    private IApartmentService apartmentService;

    /**
     * 查询公寓管理列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Apartment apartment)
    {
        startPage();
        List<Apartment> list = apartmentService.selectApartmentList(apartment);
        return getDataTable(list);
    }

    @GetMapping("/facilities")
    public List<Long> getFacilityIdsByApartmentId(Long facilityIds) {
        return apartmentService.findApartmentsByFacilityIds(facilityIds);
    }
    /**
     * 导出公寓管理列表
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:export')")
    @Log(title = "公寓管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Apartment apartment)
    {
        List<Apartment> list = apartmentService.selectApartmentList(apartment);
        ExcelUtil<Apartment> util = new ExcelUtil<Apartment>(Apartment.class);
        util.exportExcel(response, list, "公寓管理数据");
    }

    /**
     * 获取公寓管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(apartmentService.selectApartmentById(id));
    }


    /**
     * 新增公寓管理
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:add')")
    @Log(title = "公寓管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Apartment apartment)
    {
        return toAjax(apartmentService.insertApartment(apartment));
    }

    /**
     * 修改公寓管理
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:edit')")
    @Log(title = "公寓管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Apartment apartment)
    {
        return toAjax(apartmentService.updateApartment(apartment));
    }

    /**
     * 删除公寓管理
     */
    @PreAuthorize("@ss.hasPermi('ihome:apartment:remove')")
    @Log(title = "公寓管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(apartmentService.deleteApartmentByIds(ids));
    }

    @GetMapping("/nearBy")
    public List<Object> selectLocal(String street, Double longitude, Double latitude) {
        return apartmentService.selectLocal(street, longitude, latitude);
    }
}
